package com.bifrost.model.sap;

import com.bifrost.entity.Order;
import com.bifrost.entity.OrderReturn;
import com.bifrost.entity.TransactionSendLog;
import com.bifrost.util.DateTimeUtil;
import com.thoughtworks.xstream.annotations.XStreamAlias;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * Author by yanxiaobo
 * Date on 2018/5/29$.
 */
@XStreamAlias("HEADER")
public class ReportHeader extends BaseXml implements Serializable {

    public static final String ZSJLY_WECHAT_MALL_WECHAT_PAY = "1";
    public static final String ZSYFS_WECHAT_MALL_WECHAT_PAY = "27";

    public static final String NAME_SPACE_PREFIX = "<ns0:MT_SEND_POSSYPAY xmlns:ns0=\"urn:cb:bhwsc:send_possypay_out\">";
    public static final String NAME_SPACE_SUFFIX = "</ns0:MT_SEND_POSSYPAY>";

    public static int LAST_SEND_DATE_DAY = 25;
    public static int LAST_SEND_DATE_HOUR = 23;
    public static int LAST_SEND_DATE_MINUTE = 59;
    public static int LAST_SEND_DATE_SECOND = 59;


    //TODO 是log的创建日期
    private String DATUM;//
    private String WERKS;//地点
    private Long ZHXMH ;//行项目号
    private String ZSJLY = ZSJLY_WECHAT_MALL_WECHAT_PAY;//数据来源
    private String ZSYFS = ZSYFS_WECHAT_MALL_WECHAT_PAY;//收银方式
    private String ZZSYY;//收银员
    private String KUNNR;//客户编号
    private BigDecimal ZYS;//应收
    private BigDecimal ZYJ;//应缴
    private BigDecimal ZYXS;//预销售
    private BigDecimal ZDSSYK;//代收收银款
    private BigDecimal ZSJ;//实缴
    private BigDecimal ZYHHD;//银行回单
    private BigDecimal ZYHKSXF;//银行卡手续费
    private BigDecimal ZWCLPTH;//未处理pos退货
    private BigDecimal ZYCLPTH;//已处理pos退货
    private BigDecimal ZYDTHHJE;//异地退换货金额
    private String ZYDTHHDW;//异地退换货单位
    private BigDecimal ZTZ;//调整
    private BigDecimal ZCC1;//差错1
    private BigDecimal ZCC2;//差错2
    private String ZCJRQ;//创建日期  TODO：是上传时间
    private String TRANSNUMBER;

    public ReportHeader() {

    }

    public <T> ReportHeader(T o, TransactionSendLog log) {
        this.TRANSNUMBER = log.getTransNumber();
        this.ZHXMH=log.getId();
        this.DATUM = DateTimeUtil.dateToStr(log.getCreateDate(),"yyyyMMdd");
        this.WERKS = log.getStroeCode();

        this.ZCJRQ  = DateTimeUtil.dateToStr(getSendDate(),"yyyyMMdd");


        if(!(o instanceof Order || o instanceof OrderReturn))
            throw new IllegalArgumentException("参数：" + o + " 类型错误，期望的类型是Order或者OrderReturn");
        if(log.getType() == TransactionSendLog.Type.SALES) {
            Order order = (Order) o;
            this.ZYS = order.getOrderTotal();
            this.ZYJ = order.getOrderTotal();
            this.ZSJ = order.getOrderTotal();
        } else {
            BigDecimal var = null;
            if(log.getType() == TransactionSendLog.Type.REFUND) {
                OrderReturn orderReturn = (OrderReturn)o;
                var = orderReturn.getRefundTotal();
            } else {
                Order cancel = (Order)o;
                var = cancel.getOrderTotal();
            }
            var = new BigDecimal("0").subtract(var);
            this.ZYS = var;
            this.ZYJ = var;
            this.ZSJ = var;

        }
    }

    public Date getSendDate() {
        Date lastSendDate = DateTimeUtil.getsPecificDate(LAST_SEND_DATE_DAY,
                LAST_SEND_DATE_HOUR,LAST_SEND_DATE_MINUTE,LAST_SEND_DATE_SECOND);
        Date sendDate = new Date();

        if(lastSendDate.compareTo(sendDate) < 0)//最后发送日期在当前日期之后
            sendDate = lastSendDate;

        return sendDate;
    }
}
